//package keer.httpinterface.http;
//
//import keer.httpinterface.HttpInterfaceException;
//import keer.httpinterface.utils.CacheMap;
//
//import java.net.MalformedURLException;
//import java.net.URL;
//import java.util.concurrent.*;
//
//public class PooledURLHttpRequester extends AbstractHttpRequester {
//    private CacheMap<String, ThreadPoolExecutor> poolCache;
//    private HttpProperties properties;
//
//
//    @Override
//    protected void configure0(HttpProperties properties) throws Exception {
//        this.properties = properties;
//        this.poolCache = new CacheMap<>();
//    }
//
//    @Override
//    protected HttpResponse request0(HttpRequest request) throws Exception {
//        ThreadPoolExecutor executor = getPool(request.getUrl());
//        Future<HttpResponse> response = executor.submit(() -> {
//            RequesterThread t = (RequesterThread) Thread.currentThread();
//            return t.request.request0(request);
//        });
//        try {
//            return response.get();
//        } catch (Exception e) {
//            throw new HttpInterfaceException("Http request failure.", e);
//        }
//    }
//
//    private ThreadPoolExecutor getPool(String url) throws MalformedURLException {
//        URL u = new URL(url);
////        String host = u.getHost();
////        int port = u.getPort();
//        String host = u.getProtocol() + "://" + u.getHost() + (u.getPort() > 0 ? ":" + u.getPort() : "");
//        ThreadFactory factory = crateThreadFactory(properties);
//        return poolCache.loadOrCreate(host, () -> new ThreadPoolExecutor(properties.getInitialConnections(),
//                properties.getMaxConnections(),
//                properties.getWaitConnectionTimeoutMillis(),
//                TimeUnit.MILLISECONDS,
//                new LinkedBlockingQueue<>(),
//                factory,
//                new ThreadPoolExecutor.AbortPolicy()
//                ));
//    }
//
//    private static ThreadFactory crateThreadFactory(HttpProperties properties) throws MalformedURLException {
//        return r -> {
//            try {
//                return new RequesterThread(r, properties);
//            } catch (HttpInterfaceException e) {
//                throw new RuntimeException(e);
//            }
//        };
//    }
//
//
//    private static  class RequesterThread extends Thread {
//        private final URLHttpRequester request;
//        private final Runnable target;
//
//        private RequesterThread(Runnable target, HttpProperties properties) throws HttpInterfaceException {
//            this.request = new URLHttpRequester();
//            this.request.configure(properties);
//            this.target = target;
//        }
//        @Override
//        public void run() {
//            target.run();
//        }
//    }
//}
